<?php

include_once(APP_URL.'protected/myUtil/CheckUtil.php');
class WifiController extends Controller {
    
    public function actionPostAdd() {
        // http://112.74.203.16/wifishare_server/index.php?r=wifi/postadd

        // check param
        $userId = CheckUtil::checkPostParam(110901, 'userId', false);
        $userSession = CheckUtil::checkPostParam(110901, 'session', false);
        $wifiSSID = CheckUtil::checkPostParam(110901, 'wifiSSID', false);
        $wifiPwd = CheckUtil::checkPostParam(110901, 'wifiPassword', false);
        $wifiMac = CheckUtil::checkPostParam(110901, 'wifiMac', false);
        $wifiLongitude = CheckUtil::checkPostParam(110901, 'wifiLongitude', false);
        $wifiLatitude = CheckUtil::checkPostParam(110901, 'wifiLatitude', false);
        $wifiAreaid = CheckUtil::checkPostParam(110901, 'wifiAreaId', false);
        $wifiAddress = CheckUtil::checkPostParam(110901, 'wifiAddress', false);
        
        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(110901, 'user not exsit !');
            exit;
        }
        
        $conditionArr = array(
            'condition' => 'wifi_mac=:wifiMac',
            'params' => array('wifiMac'=>$wifiMac)
        );// fill condition
        $TblWifi = new TblWifi();
        $result = $TblWifi->selectRecord($conditionArr);
        if ($result) {// update record
            if ($wifiSSID == $result->wifi_ssid && $wifiPwd == $result->wifi_pwd) {
                echo EchoUtil::retFailed(110903, 'wifi has been shared !');
                exit;
            }
            $conditionArr = array(
                'condition' => 'wifi_id=:wifiId',
                'params' => array('wifiId'=>$result->wifi_id)
            );// fill condition
            $contentArr = array(
                'wifi_uid' => $userId,
                'wifi_mac' => $wifiMac,
                'wifi_ssid' => $wifiSSID,
                'wifi_pwd' => $wifiPwd,
                'wifi_longitude' => $wifiLongitude,
                'wifi_latitude' => $wifiLatitude,
                'wifi_areaid' => $wifiAreaid,
                'wifi_address' => $wifiAddress,
                'wifi_st' => 2,
                'wifi_sttype' => 0
            );// fill content
            if (!$TblWifi->updateRecord($conditionArr, $contentArr)) {
                echo EchoUtil::retFailed(110901, 'server error !');
                exit;
            }
        } else {// add record
            $contentArr = array(
                'wifi_uid' => $userId,
                'wifi_mac' => $wifiMac,
                'wifi_ssid' => $wifiSSID,
                'wifi_pwd' => $wifiPwd,
                'wifi_longitude' => $wifiLongitude,
                'wifi_latitude' => $wifiLatitude,
                'wifi_areaid' => $wifiAreaid,
                'wifi_address' => $wifiAddress,
                'wifi_st' => 2,
                'wifi_sttype' => 0
            );// fill content
            if (!$TblWifi->addRecord('wifi', $contentArr)) {
                echo EchoUtil::retFailed(110901, 'server error !');
                exit;
            }
        }
        
        // return success
        echo EchoUtil::msgSuccess(1, (object)array());
        
    }
    
    public function actionPostDelete() {
        // http://112.74.203.16/wifishare_server/index.php?r=wifi/postdelete
    
        // check param
        $userId = CheckUtil::checkPostParam(111001, 'userId', false);
        $userSession = CheckUtil::checkPostParam(111001, 'session', false);
        $wifiId = CheckUtil::checkPostParam(111001, 'wifiId', false);
    
        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(111001, 'user not exsit !');
            exit;
        }
    
        // wifi not exsit
        $TblWifi = new TblWifi();
        $conditionArr = array(
            'condition' => 'wifi_id=:wifiId',
            'params' => array('wifiId'=>$wifiId)
        );// fill condition
        $TblWifi = new TblWifi();
        $result = $TblWifi->selectRecord($conditionArr);
        if (!$result) {// wifi not exsit
            echo EchoUtil::retFailed(111003, 'wifi not exsit !');
            exit;
        }
         
        // no right to delete wifi
        if ($userId != $result->wifi_uid) {
            echo EchoUtil::retFailed(111004, 'no right to delete wifi !');
            exit;
        }
         
        // delete wifi
        if (!$TblWifi->deleteRecord($conditionArr)) {
            echo EchoUtil::retFailed(111002, 'delete wifi failed !');
            exit;
        }
    
        // return success
        echo EchoUtil::msgSuccess(1, (object)array());
    
    }
    
    public function actionPostUpdate() {
        // http://112.74.203.16/wifishare_server/index.php?r=wifi/postupdate
    
        // check param
        $userId = CheckUtil::checkPostParam(111101, 'userId', false);
        $userSession = CheckUtil::checkPostParam(111101, 'session', false);
        $wifiId = CheckUtil::checkPostParam(111101, 'wifiId', false);
        $wifiSSID = CheckUtil::checkPostParam(111101, 'wifiSSID', false);
        $wifiPwd = CheckUtil::checkPostParam(111101, 'wifiPassword', false);
        $wifiLongitude = CheckUtil::checkPostParam(111101, 'wifiLongitude', false);
        $wifiLatitude = CheckUtil::checkPostParam(111101, 'wifiLatitude', false);
        $wifiAreaid = CheckUtil::checkPostParam(111101, 'wifiAreaId', false);
        $wifiAddress = CheckUtil::checkPostParam(111101, 'wifiAddress', false);
         
        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(111101, 'user not exsit !');
            exit;
        }
    
        // wifi not exsit
        $TblWifi = new TblWifi();
        $conditionArr = array(
            'condition' => 'wifi_id=:wifiId',
            'params' => array('wifiId'=>$wifiId)
        );// fill condition
        $TblWifi = new TblWifi();
        $result = $TblWifi->selectRecord($conditionArr);
        if (!$result) {// wifi not exsit
            echo EchoUtil::retFailed(111103, 'wifi not exsit !');
            exit;
        }
         
        // no right to update wifi
        if ($userId != $result->wifi_uid) {
            echo EchoUtil::retFailed(111104, 'no right to update wifi !');
            exit;
        }

        $conditionArr = array(
            'condition' => 'wifi_id=:wifiId',
            'params' => array('wifiId'=>$wifiId)
        );// fill condition
        $contentArr = array(
            'wifi_ssid' => $wifiSSID,
            'wifi_pwd' => $wifiPwd,
            'wifi_longitude' => $wifiLongitude,
            'wifi_latitude' => $wifiLatitude,
            'wifi_areaid' => $wifiAreaid,
            'wifi_address' => $wifiAddress
        );// fill content
        if (!$TblWifi->updateRecord($conditionArr, $contentArr)) {
            echo EchoUtil::retFailed(110901, 'server error !');
            exit;
        }
    
        // return success
        echo EchoUtil::msgSuccess(1, (object)array());
    
    }
    
    public function actionPostSt() {
        // http://112.74.203.16/wifishare_server/index.php?r=wifi/postst
        
        // {"wifiList":[{"wifiId":2,"invalidType":1},{"wifiId":3,"invalidType":2}]}
        
        // check param
        $paramObj = json_decode(CheckUtil::checkPostParam(110101, 'wifiList', false));
        if (!is_object($paramObj) || empty($paramObj)) {
            echo EchoUtil::retFailed(110101, 'param invalid !');
            exit;
        }
        
        $wifiList = $paramObj->wifiList;
        $TblWifiObj = new TblWifi();
        foreach ($wifiList as $value) {
            $conditionArr = array(
                'condition' => 'wifi_id=:wifiId',
                'params' => array('wifiId'=>$value->wifiId)
            );// fill condition
            $contentArr = array(
                'wifi_st' => 1,
                'wifi_sttype' => $value->invalidType,
                
            );// fill content
            $result = $TblWifiObj->updateRecord($conditionArr, $contentArr);// update record
            if (!$result) {// update failed
                echo EchoUtil::retFailed(110102, 'post failed !');
                exit;
            }
        }

        // return success
        echo EchoUtil::retSucc(1, (object)array());// return success
        
    }
    
    public function actionGetNearList() {
        // http://112.74.203.16/wifishare_server/index.php?r=wifi/getnearlist
        
        // check param
        $userId = CheckUtil::checkGetParam(100201, 'userId', false);
        $userSession = CheckUtil::checkGetParam(100201, 'session', false);
        $wifiAreaid = CheckUtil::checkGetParam(100201, 'areaId', false);
        $longitude = CheckUtil::checkGetParam(100201, 'longitude', false);
        $latitude = CheckUtil::checkGetParam(100201, 'latitude', false);
         
        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(100201, 'user not exsit !');
            exit;
        }
        
        // filter accord to distance
        $latitudeMin = $latitude - WIFI_LBS_DISTANCE / WIFI_LBS_PARAM;
        $latitudeMax = $latitude + WIFI_LBS_DISTANCE / WIFI_LBS_PARAM;
        $longitudeMin = $longitude + WIFI_LBS_DISTANCE / (WIFI_LBS_PARAM * cos($latitude));
        $longitudeMax = $longitude - WIFI_LBS_DISTANCE / (WIFI_LBS_PARAM * cos($latitude));

        $criteria = new CDbCriteria();
        $criteria->select = '*';
        $criteria->order = 'wifi_id DESC';
        $criteria->condition = 'wifi_areaid=:wifiAreaid';
        $criteria->params = array(':wifiAreaid' => $wifiAreaid);
        $criteria->addBetweenCondition('wifi_latitude', $latitudeMin, $latitudeMax);
        $criteria->addBetweenCondition('wifi_longitude', $longitudeMin, $longitudeMax);
        $TblWifi = new TblWifi();
        $result = $TblWifi->selectRecordAll2($criteria);
        if(!$result) {
            echo EchoUtil::retFailed(100202, 'get near list failed !');
            exit;
        }
        
        foreach ($result as $key=>$value) {
            $dataArray[] = array(
                'wifiId' => $value->wifi_id,
                'wifiMasterId' => $value->wifi_uid,
                'wifiStatus' => $value->wifi_st,
                'wifiSSID' => $value->wifi_ssid,
                'wifiPassword' => $value->wifi_pwd,
                'wifiMac' => $value->wifi_mac,
                'wifiLongitude' => $value->wifi_longitude,
                'wifiLatitude' => $value->wifi_latitude,
                'wifiAreaId' => $value->wifi_areaid,
                'wifiAddress' => $value->wifi_address
            );
        }
        
        // return success
        echo EchoUtil::msgSuccess(1, array('list'=>$dataArray));
        
    }

	public function actionGetMeList() {
	    // http://112.74.203.16/wifishare_server/index.php?r=wifi/getmelist
	
	    // check param
	    $userId = CheckUtil::checkGetParam(100501, 'userId', false);
	    $userSession = CheckUtil::checkGetParam(100501, 'session', false);
	    
	    // identify user
	    $result = $this->userIdentify($userId, $userSession);
	    if (!$result) {
	        echo EchoUtil::retFailed(100501, 'user not exsit !');
	        exit;
	    }
	     
	    
	    // wifi not exsit
	    $TblWifi = new TblWifi();
	    $conditionArr = array(
	        'select' => array('*'),
            'order' => 'wifi_id DESC',
	        'condition' => 'wifi_uid=:wifiUid',
	        'params' => array('wifiUid'=>$userId)
	    );// fill condition
	    $TblWifi = new TblWifi();
	    $result = $TblWifi->selectRecordAll($conditionArr);
	    if (!$result) {// wifi not exsit
	        echo EchoUtil::retFailed(100602, 'get me list failed !');
	        exit;
	    }
	    foreach ($result as $key=>$value) {
	        $dataArray[] = array(
	            'wifiId' => $value->wifi_id,
	            'wifiMasterId' => $value->wifi_uid,
	            'wifiStatus' => $value->wifi_st,
	            'wifiSSID' => $value->wifi_ssid,
	            'wifiPassword' => $value->wifi_pwd,
	            'wifiMac' => $value->wifi_mac,
	            'wifiLongitude' => $value->wifi_longitude,
	            'wifiLatitude' => $value->wifi_latitude,
	            'wifiAreaId' => $value->wifi_areaid,
	            'wifiAddress' => $value->wifi_address
	        );
	    }
	    
	    // return success
	    echo EchoUtil::msgSuccess(1, array('list'=>$dataArray));
	    
	}

	public function actionGetDetail() {
	    // http://112.74.203.16/wifishare_server/index.php?r=wifi/getdetail
	
	    // check param
	    $userId = CheckUtil::checkGetParam(100601, 'userId', false);
	    $userSession = CheckUtil::checkGetParam(100601, 'session', false);
	    $wifiId = CheckUtil::checkGetParam(100601, 'wifiId', false);
	    
	    // identify user
	    $result = $this->userIdentify($userId, $userSession);
	    if (!$result) {
	        echo EchoUtil::retFailed(100601, 'user not exsit !');
	        exit;
	    }
	    

	    // wifi not exsit
	    $TblWifi = new TblWifi();
	    $conditionArr = array(
	        'select' => array('*'),
	        'condition' => 'wifi_id=:wifiId',
	        'params' => array('wifiId'=>$wifiId)
	    );// fill condition
	    $TblWifi = new TblWifi();
	    $result = $TblWifi->selectRecord($conditionArr);
	    if (!$result) {// wifi not exsit
	        echo EchoUtil::retFailed(100602, 'wifi not exsit !');
	        exit;
	    }
	     
	    // return success
	    echo EchoUtil::msgSuccess(1, array(
	            'wifiId' => $result->wifi_id,
	            'wifiMasterId' => $result->wifi_uid,
	            'wifiStatus' => $result->wifi_st,
	            'wifiSSID' => $result->wifi_ssid,
	            'wifiPassword' => $result->wifi_pwd,
	            'wifiMac' => $result->wifi_mac,
	            'wifiLongitude' => $result->wifi_longitude,
	            'wifiLatitude' => $result->wifi_latitude,
	            'wifiAreaId' => $result->wifi_areaid,
	            'wifiAddress' => $result->wifi_address
	        ));
	    
	}

	public function actionGetProfitList() {
	    // http://112.74.203.16/wifishare_server/index.php?r=wifi/getprofitlist
	
	    // check param
	    $userId = CheckUtil::checkGetParam(100701, 'userId', false);
	    $userSession = CheckUtil::checkGetParam(100701, 'session', false);
	
	    // identify user
	    $result = $this->userIdentify($userId, $userSession);
	    if (!$result) {
	        echo EchoUtil::retFailed(100701, 'user not exsit !');
	        exit;
	    }
	
	    // select profit list
	    $TblProfit = new TblProfit();
	    $conditionArr = array(
	        'select' => array('*'),
	        'order' => 'profit_id DESC',
	        'condition' => 'profit_uid=:profitUid',
	        'params' => array('profitUid'=>$userId)
	    );// fill condition
	    $result = $TblProfit->selectRecordAll($conditionArr);
	    if(!$result) {
	        echo EchoUtil::retFailed(100702, 'get wifi profit list failed !');
	        exit;
	    }
	    $TblWifi = new TblWifi();
	    foreach ($result as $key=>$value) {
	        $conditionArr = array(
	            'select' => array('wifi_ssid'),
	            'condition' => 'wifi_id=:wifiId',
	            'params' => array('wifiId'=>$value->profit_wid)
	        );// fill condition
	        $resultWifi = $TblWifi->selectRecord($conditionArr);
	        if ($resultWifi) {
	            $dataArray[] = array(
	                'wifiId' => $value->profit_wid,
	                'wifiSSID' => $resultWifi->wifi_ssid,
	                'wifiProfit' => $value->profit_money
	            );
	        }
	    }
	     
	    // return success
	    echo EchoUtil::msgSuccess(1, array('list'=>$dataArray));
	
	}
	
	public function actionGetProfitDetail() {
	    // http://112.74.203.16/wifishare_server/index.php?r=wifi/getprofitdetail
	
	    // check param
	    $userId = CheckUtil::checkGetParam(100801, 'userId', false);
	    $userSession = CheckUtil::checkGetParam(100801, 'session', false);
	    $wifiId = CheckUtil::checkGetParam(100801, 'wifiId', false);
	
	    // identify user
	    $result = $this->userIdentify($userId, $userSession);
	    if (!$result) {
	        echo EchoUtil::retFailed(100801, 'user not exsit !');
	        exit;
	    }
	
	    // select profit list
	    $TblProfit = new TblProfit();

	    $conditionArr = array(
	        'select' => array('*'),
	        'order' => 'profit_month ASC',
	        'condition' => 'profit_wid=:profitWid',
	        'params' => array('profitWid'=>$wifiId)
	    );// fill condition
	    $result = $TblProfit->selectRecordAll($conditionArr);
	    if(!$result) {
	        echo EchoUtil::retFailed(100802, 'get wifi profit detail failed !');
	        exit;
	    }
	
	    foreach ($result as $key=>$value) {
	        $dataArray[] = array(
	            'month' => $value->profit_month,
	            'wifiProfit' => $value->profit_money
	        );
	    }
	    
	    // return success
	    echo EchoUtil::msgSuccess(1, array('list'=>$dataArray));
	
	}
	
}
